﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;
using System.IO;

namespace WOWMultiPlay
{
    public partial class Form_ChooseInstanceAndProfile : Form
    {
        public Form_ChooseInstanceAndProfile()
        {
            InitializeComponent();
        }

        private void Form_ChooseInstanceAndProfile_Load(object sender, EventArgs e)
        {
            LoadInstances();
            LoadProfiles();
        }

        private bool IsLoadingInstances = false;
        private DataTable DTInstances;

        private void LoadInstances()
        {
            if (!IsLoadingInstances)
            {
                IsLoadingInstances = true;

                DTInstances = new DataTable();
                DTInstances.Columns.Add("ProcessDisplayTitle");
                DTInstances.Columns.Add("ProcessMainWindowHandle");

                Process[] processes = Process.GetProcessesByName(Settings.UserSettings.ProcessName);

                foreach (Process P in processes)
                {
                    DataRow DR = DTInstances.NewRow();
                    DR["ProcessDisplayTitle"] = P.MainWindowTitle + " <" + P.MainWindowHandle + ">";
                    DR["ProcessMainWindowHandle"] = P.MainWindowHandle;

                    DTInstances.Rows.Add(DR);
                }

                listBox_Instances.DataSource = DTInstances;
                listBox_Instances.DisplayMember = "ProcessDisplayTitle";
                listBox_Instances.ValueMember = "ProcessMainWindowHandle";

                IsLoadingInstances = false;
            }
        }

        private void listBox_Instances_DoubleClick(object sender, EventArgs e)
        {
            if (listBox_Instances.SelectedItems.Count > 0)
            {
                IntPtr mainWindowHanle = new IntPtr(Convert.ToInt32(listBox_Instances.SelectedValue));

                WindowsAPI.ShowWindowAsync(mainWindowHanle, WindowsAPI.WS.WS_SHOWNORMAL);
                WindowsAPI.SetForegroundWindow(mainWindowHanle);
            }
        }

        private bool IsLoadingProfiles = false;
        private DataTable DTProfiles;

        private void LoadProfiles()
        {
            if (!IsLoadingProfiles)
            {
                IsLoadingProfiles = true;

                DTProfiles = new DataTable();
                DTProfiles.Columns.Add("ProfileFileName");
                DTProfiles.Columns.Add("ProfileFilePath");

                DirectoryInfo dictInfo = new DirectoryInfo(Settings.ProfilesFolderPath);
                if (!dictInfo.Exists)
                {
                    Directory.CreateDirectory(Settings.ProfilesFolderPath);
                }

                FileInfo[] filesInfo = dictInfo.GetFiles("*." + Settings.ProfilesFileSuffix);

                foreach (FileInfo FileInfo in filesInfo)
                {
                    DataRow dr = DTProfiles.NewRow();
                    dr["ProfileFileName"] = FileInfo.Name;
                    dr["ProfileFilePath"] = FileInfo.FullName;

                    DTProfiles.Rows.Add(dr);
                }

                //New Profile
                DataRow dr_NewProfile = DTProfiles.NewRow();
                dr_NewProfile["ProfileFileName"] = "New Profile...";
                dr_NewProfile["ProfileFilePath"] = dr_NewProfile["ProfileFileName"];
                DTProfiles.Rows.Add(dr_NewProfile);

                listBox_Profiles.DataSource = DTProfiles;
                listBox_Profiles.DisplayMember = "ProfileFileName";
                listBox_Profiles.ValueMember = "ProfileFilePath";

                IsLoadingProfiles = false;
            }
        }

        private void listBox_Profiles_DoubleClick(object sender, EventArgs e)
        {
            SelectInstanceAndProfile();
        }

        private void SelectInstanceAndProfile()
        {
            //Selected the last item: New Profile... so ...
            if (listBox_Profiles.SelectedIndex == listBox_Profiles.Items.Count - 1)
            {
                Dialog_NewProfile.DefaultExt = Settings.ProfilesFileSuffix;
                Dialog_NewProfile.Filter = "Profile (*." + Settings.ProfilesFileSuffix + ")|*.*";
                Dialog_NewProfile.InitialDirectory = Settings.ProfilesFolderPath;
                Dialog_NewProfile.ShowDialog(this);

                LoadProfiles();
            }
            else
            {
                if (listBox_Instances.CheckedItems.Count > 0)
                {
                    //Settings.Instances = new IntPtr(Convert.ToInt32(listBox_Instances.SelectedValue));
                    foreach (int checkedIndex in listBox_Instances.CheckedIndices)
                    {
                        Settings.Instances.Add(new IntPtr(Convert.ToInt32(DTInstances.Rows[checkedIndex]["ProcessMainWindowHandle"])));
                    }
                    Settings.LoadedInstance = true;
                }

                if (listBox_Profiles.SelectedItems.Count > 0)
                {
                    Settings.Profile = new mpProfile(listBox_Profiles.SelectedValue.ToString());
                    Settings.LoadedProfile = true;
                }

                if (Settings.LoadedInstance == true && Settings.LoadedProfile == true)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }

        private void Dialog_NewProfile_FileOk(object sender, CancelEventArgs e)
        {
            mpProfile mpProfile = new mpProfile();
            mpProfile.ProfileName = (new FileInfo(Dialog_NewProfile.FileName)).Name;
            mpProfile.SaveProfile(Dialog_NewProfile.FileName);
        }

        private void ToolStripMenuItem_Options_Click(object sender, EventArgs e)
        {
            Form_Options _Form_Options = new Form_Options();
            _Form_Options.ShowDialog();
        }

        private void toolStripStatusLabel_RefreshInstance_Click(object sender, EventArgs e)
        {
            this.LoadInstances();
        }
    }
}